import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.net.InetAddress;

/**
 * A JDialog serving as a graphical extension to create
 * a Webserver within Webster.
 *
 * @author Marlon Etheredge
 * @author <a href="mailto:m.etheredge@gmail.com">m.etheredge@gmail.com</a>
 * @author <a href="http://www.foxage.nl/">http://www.foxage.nl</a>
 */
public class CreateServerDialog extends JDialog {
    
    /** Parent */
    private WebsterGUI gui;
    
    //Labels
    private JLabel portnumberLabel;
    private JLabel hostsLabel;
    
    //Fields
    private JTextField portnumber;
    private JComboBox hosts;
    
    //Buttons
    private JButton createButton;
    
    /**
     * Construct a new CreateServerDialog with frame as it's parent.
     *
     * @param frame The parent JFrame.
     */
    public CreateServerDialog(WebsterGUI gui) {
        super((JFrame)gui);
        this.gui = gui;
        this.addComponents();
    }
    
    /**
     * Adds a components to this instance of CreateServerDialog.
     */
    private void addComponents() {
        getContentPane().setLayout(null);
        this.setResizable(false);
        
        this.portnumberLabel = new JLabel("Portnumber");
        this.portnumberLabel.setBounds(10, 12, 157, 22);
        this.getContentPane().add(this.portnumberLabel);

        this.portnumber = new JTextField();
        this.portnumber.setBounds(146, 12, 157, 22);
        this.getContentPane().add(this.portnumber);

        this.hostsLabel = new JLabel("Host");
        this.hostsLabel.setBounds(10, 46, 157, 22);
        this.getContentPane().add(this.hostsLabel);

        DefaultComboBoxModel hostsModel = new DefaultComboBoxModel(Webster.getAllLocalBindAddresses());
        this.hosts = new JComboBox();
        this.hosts.setModel(hostsModel);
        this.hosts.setBounds(146, 46, 157, 22);
        this.getContentPane().add(this.hosts);

        this.createButton = new JButton("Create server");
        this.createButton.setBounds(10, 80, 157, 22);
        this.getContentPane().add(createButton);
        this.createButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent event) {
				int portnumber = 0;        
			
				try {
					    portnumber = Integer.parseInt(CreateServerDialog.this.portnumber.getText());
				} catch(NumberFormatException exception) {
					    CreateServerDialog.this.dispose();

					    JOptionPane.showMessageDialog(CreateServerDialog.this.gui, "Portnumber is not a number, try again");
				}

				Webster.createWebserver((InetAddress) CreateServerDialog.this.hosts.getSelectedItem(), portnumber);
				CreateServerDialog.this.dispose();        
				JOptionPane.showMessageDialog(CreateServerDialog.this.gui, "Created webserver");
				CreateServerDialog.this.gui.updateServerTree();
			}
        });

        this.pack();

        this.setSize(325, 147);
        this.setVisible(true);
        this.setTitle("Create a server");
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
}
